using UnityEngine;
using Yoozoo.Libs;
using Yoozoo.Managers;

namespace Gameplay.PVE.TurnBase
{
    public class ElementBase : IClass
    {
        public TbsSkillElementConfig config;
        public BattleUnit target;//目标
        public BattleUnit source;//来源
       
        public bool IsFree;
        public int endRound;
        protected float endTime;

        protected GameObject effect;
        
        void IClass.OnReset()
        {
            endTime = 0;
        }
        
        /// <summary>
        /// 执行
        /// </summary>
        public virtual void Execute()
        {
            if (config.rgbIntensity != 0)
            {
                var arg = ClassManager.Get<ChangeColorArg>();
                arg.rgb = config.rgb;
                arg.rgb.w = 1;
                arg.amount = config.rgbIntensity;
                target.Transmit(ETransmitType.AddColor,arg);
            }
            ShowEffect();
        }
        
        /// <summary>
        /// 每帧更新
        /// </summary>
        public virtual void Update()
        {
            if (endTime != 0 && endTime < TimeManager.logicTime)
            {
                IsFree = true;
            }
        }
        
        /// <summary>
        /// 设置目标
        /// </summary>
        public virtual void SetTarget(BattleUnit target,BattleUnit source)
        {
            this.target = target;
            this.source = source;
        }
        
        /// <summary>
        /// 解析元素内容
        /// </summary>
        public virtual void Parse(TbsSkillElementConfig config)
        {
            IsFree = false;
            this.config = config;
            var lastTime = GetValue("LastTime") / 1000f;
            endTime = lastTime == 0 ? 0 : TimeManager.logicTime + lastTime;
            var lastRound = GetValue("LastRound");
            endRound = lastRound == 0 ? 0 : TurnBaseManager.Instance.GetRound() + lastRound - 1;
        }

        /// <summary>
        /// 播放特效
        /// </summary>
        protected virtual void ShowEffect()
        {
            if (config.effectId != 0 && !target.Data.isDead)
            {
                effect = PveResourceManager.GetResource(PveResourceManager.EPveResType.SkillEffect, config.effectId);
                if (effect != null)
                {
                    effect.transform.SetParent(target.gameObject.transform);
                    effect.transform.localPosition = Vector3.zero;
                }
            }
        }

        protected virtual void HideEffect()
        {
            if (effect != null)
            {
                PveResourceManager.RecoverResource(effect,PveResourceManager.EPveResType.SkillEffect, config.effectId);
                effect = null;
            }
        }
        
        /// <summary>
        /// 销毁
        /// </summary>
        public virtual void Free()
        {
            if (config.rgbIntensity != 0)
            {
                var arg = ClassManager.Get<ChangeColorArg>();
                arg.rgb = config.rgb;
                arg.rgb.w = 1;
                arg.amount = config.rgbIntensity;
                target.Transmit(ETransmitType.RemoveColor,arg);
            }

            HideEffect();
        }
        
        public int GetValue(string key)
        {
            if (config.element_param.TryGetValue(key, out int value))
            {
                return value;
            }
            return 0;
        }
    }
}